﻿@inherits Fluxor.Blazor.Web.Components.FluxorComponent
@using SimpleIdServer.IdServer.Api.AuthenticationSchemeProviders;
@using SimpleIdServer.IdServer.Website.Stores.IdProviderStore;
@using SimpleIdServer.IdServer.Website.Resources;
@inject DialogService dialogService
@inject NotificationService notificationService
@inject IDispatcher dispatcher

<RadzenSteps>
    <Steps>
        <RadzenStepsItem Text="@Global.MapperType">
            <div class="row gy-4">
                @foreach (var mapperType in MapperTypes)
                {
                    <div class="col-md-6">
                        <RadzenCard @onclick="() => SelectMapper(mapperType)" class="@(SelectedMapperType != null && SelectedMapperType.Name == mapperType.Name ? "selected selectable" : "selectable")">
                            <div class="row">
                                <div class="col-md-3">
                                    <RadzenImage Path="@mapperType.PictureUrl" Style="width: 60px" />
                                </div>
                                <div class="col">
                                    <h5>@mapperType.Name</h5>
                                    <p class="text-muted">@(new MarkupString(mapperType.ShortDescription))</p>
                                </div>
                            </div>
                        </RadzenCard>
                    </div>
                }
            </div>
        </RadzenStepsItem>
        <RadzenStepsItem Text="Create" Disabled=@(SelectedMapperType == null)>
            @switch(SelectedMapperType.Type)
            {
                case MappingRuleTypes.USERATTRIBUTE:
                    <UserAttributeIdProviderMapper AuthProviderMapperUpdated=@UpdateAuthProviderMapper></UserAttributeIdProviderMapper>
                    break;
                case MappingRuleTypes.USERPROPERTY:
                    <UserPropertyIdProviderMapper AuthProviderMapperUpdated=@UpdateAuthProviderMapper></UserPropertyIdProviderMapper>
                    break;
                case MappingRuleTypes.IDENTIFIER:
                    <UserUniqueIdProviderMapper Type=MappingRuleTypes.IDENTIFIER AuthProviderMapperUpdated=@UpdateAuthProviderMapper></UserUniqueIdProviderMapper>
                    break;
                case MappingRuleTypes.SUBJECT:
                    <UserUniqueIdProviderMapper Type=MappingRuleTypes.SUBJECT AuthProviderMapperUpdated=@UpdateAuthProviderMapper></UserUniqueIdProviderMapper>
                    break;
            }
        </RadzenStepsItem>
    </Steps>
</RadzenSteps>

@code {
    [Parameter]
    public string IdProviderName { get; set; }

    record MapperType
    {
        public string Name { get; set; } = null!;
        public string ShortDescription { get; set; } = null!;
        public string Description { get; set; } = null!;
        public MappingRuleTypes Type { get; set; }
        public string PictureUrl { get; set; } = null!;
    }

    ICollection<MapperType> MapperTypes { get; set; } = new List<MapperType>
    {
        new MapperType { Name = Global.Attribute, ShortDescription = Global.UserAttribute, Description = Global.MapClaimToUserAttribute, Type = MappingRuleTypes.USERATTRIBUTE,  PictureUrl = "_content/SimpleIdServer.IdServer.Website/images/UserAttribute.png" },
        new MapperType { Name = Global.Property, ShortDescription = Global.UserProperty, Description = Global.UserPropertyDescription, Type = MappingRuleTypes.USERPROPERTY, PictureUrl = "_content/SimpleIdServer.IdServer.Website/images/UserProperty.png" },
        new MapperType { Name = Global.Subject, ShortDescription = Global.UserSubject, Description = Global.UserSubjectDescription, Type = MappingRuleTypes.SUBJECT, PictureUrl = "_content/SimpleIdServer.IdServer.Website/images/UserSubject.png" },
        new MapperType { Name = Global.Identifier, ShortDescription = Global.UserIdentifier, Description = Global.UserIdentifierDescription, Type = MappingRuleTypes.IDENTIFIER, PictureUrl = "_content/SimpleIdServer.IdServer.Website/images/UserId.png" }
    };

    MapperType? SelectedMapperType { get; set; } = null;

    protected override Task OnAfterRenderAsync(bool firstRender)
    {
        base.OnAfterRenderAsync(firstRender);
        if(firstRender)
        {
            SubscribeToAction<AddAuthenticationSchemeProviderMapperSuccessAction>((act) =>
            {
                dialogService.Close();
                notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.IdProviderMapperAdded });
                StateHasChanged();
            });
        }

        return Task.CompletedTask;
    }

    void SelectMapper(MapperType mapperType)
    {
        SelectedMapperType = mapperType;
    }

    void UpdateAuthProviderMapper(AuthenticationSchemeProviderMapperResult mapper)
    {
        var act = new AddAuthenticationSchemeProviderMapperAction
        {
            IdProviderName = IdProviderName,
            MapperType = mapper.MapperType,
            Name = mapper.Name,
            SourceClaimName = mapper.SourceClaimName,
            TargetUserAttribute = mapper.TargetUserAttribute,
            TargetUserProperty = mapper.TargetUserProperty
        };
        dispatcher.Dispatch(act);
    }
}